<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\model\Vod;
use fast\Tree;
use think\Db;
use think\Log;

/**
 * 首页接口
 */
class Video extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 首页
     *
     */
    public function index()
    {
        $config = $this->config();
        $configs = config('site');
        $map['ishot'] = 2;
        $map['endtimesjc'] = ['>', time()];
        if (input('uid', 0) > 0) {
            $map['uid'] = input('uid');
        }
        $list = model('Video')
            ->with('users')
            ->where($map)
            ->limit(10)
            ->select();
        if ($list) {
            foreach ($list as $k => $v) {
                $list[$k]['createtime'] = date('Y-m-d H:i', $v['createtime']);
                if (isset($v['users']['avatar'])) {
                    if (strpos($v['users']['avatar'], 'http') !== false) {
                        $list[$k]['avatar'] = $v['users']['avatar'];
                    } else {
                        if ($v['users']['avatar']) {
                            $list[$k]['avatar'] = $configs['imgurl'] . $v['users']['avatar'];
                        } else {
                            $list[$k]['avatar'] = $configs['imgurl'] . '/uploads/20200523/250b3f89b40ff3714b07cc51b4c2f63d.png';
                        }
                    }
                } else {
                    $list[$k]['avatar'] = $configs['imgurl'] . '/uploads/20200523/250b3f89b40ff3714b07cc51b4c2f63d.png';
                }


            }
        }
        $data = ['config' => $config, 'doctor' => $list];
        $this->success('请求成功', $data);
    }

    public function videoRecommend()
    {
        $uid = input('uid', 0);
        $page = input('page', 1);
        $limit = input('limit', 100);
        $map = ['lx' => 1, 'status' => 'normal'];
        $map['ishot'] = 2; // 热门

        $list = DB::name('videolist')->where($map)->orderRaw('rand(), give desc, plays desc')->limit($limit)->page($page)->select();
        $total = DB::name('videolist')->where($map)->count();

        if ($list) {
            foreach ($list as $k => $v) {
                $video = DB::name('video')->order('weigh Asc,id Asc')->where(['pid' => $v['id']])->find();

                $list[$k]['like'] = 1;
                $list[$k]['iszj'] = 1;
                if (!empty($v['id']) && !empty($video)) {
                    $videolove = Db::name('videolove')->where(['uid' => $uid, 'vid' => $v['id'], 'mid' => $video['id']])->find();
                    if ($videolove) {
                        $list[$k]['like'] = 2;
                    }

                    $videorecord = Db::name('videorecord')->where(['uid' => $uid, 'vid' => $v['id'], 'mid' => $video['id']])->find();
                    if ($videorecord) {
                        $list[$k]['iszj'] = 2;
                    }
                }

                $User = \app\common\model\User::where(['id' => $v['uid']])->field('id,nickname,avatar')->find();

                $list[$k]['mid'] = $video['id'];
                $list[$k]['href'] = $this->pissrc($video['img'] . '?x-oss-process=image/resize,l_320');
                $list[$k]['img'] = $this->pissrc($video['img'] . '?x-oss-process=image/resize,l_320');
                $list[$k]['isShowProgressBarTime'] = false;
                $list[$k]['isShowimage'] = false;
                $list[$k]['isplay'] = true;
                // $list[$k]['like']=$v['give'];
                $list[$k]['like_n'] = $v['give'];
                $list[$k]['msg'] = $v['text'];
                $list[$k]['pinlun'] = [];
                $list[$k]['playIng'] = false;
                $list[$k]['playNumber'] = 0;
                $list[$k]['sms_n'] = 0;
                $list[$k]['src'] = $this->pissrc($video['videourl']);
                $list[$k]['state'] = "pause";
                $list[$k]['title'] = '《' . $v['name'] . '》';
                $list[$k]['userID'] = $User['id'];
                $list[$k]['username'] = $User['nickname'];
                //$list[$k]['_id']=md5($v['id']);
                $list[$k]['_id'] = 'vid' . $v['id'];
                $list[$k]['namets'] = $v['name'] . sprintf(__("（Total %d sections） choose >"), $v['sum']);

                $vod = Db::name('vod')->field(['id', 'video_id', 'url', 'updated_at'])->where(['video_id' => $video['id'], 'status' => Vod::VOD_STATUS_PUBLISHED, 'platform' => 'cloudflare'])->find(); //查询可用vod
                if ($vod) {
                    $list[$k]['vod'] = $vod;
                    if (!empty($list[$k]['src'])) {
                        $list[$k]['src'] = $vod['url'];
                    }
                } else {
                    $list[$k]['vod'] = null;
                }

            }
        }

        $result = array("total" => $total, "isempty" => !empty($list) ? 1 : 2, "result" => $list, "affectedDocs" => $total, "data" => $list, 'tuday' => date('Y-m-d', time()));
        return json($result);
    }

    public function videoinfo()
    {
        $vid = input('vid', 0);//视频id
        $uid = intval(input('uid')); // 用户id
        $guest = trim(input('gid')); // 访客id
        $page = input('page', 1);
        $limit = input('limit', 100);
        Db::name('videolist')->where(['id' => $vid])->setInc('views');//累加播放记录

        $map = ['status' => 'normal', 'pid' => $vid];
//        $list = DB::name('video')->where($map)->order('weigh asc,id asc')->limit($limit)->page($page)->select();
        $list = DB::name('video')->where($map)->order('weigh asc,id asc')->select();
        $total = DB::name('video')->where($map)->count();


        $gid = 0;
        if ($guest != '') {
            $guestUp = [
                'guest' => $guest,
                'useragent' => substr(request()->server('HTTP_USER_AGENT'), 0, 255),
                'referer' => $this->request->server('HTTP_REFERER'),
            ];
            $country = null;
//            $ipInfo = geoip(request()->ip())->toArray();
//            if (!empty($ipInfo['iso_code'])) {
//                $country = $ipInfo['iso_code'];
//            }
            $guestInfo = Db::name('guest')->where(['guest' => $guest])->find();
            if (empty($guestInfo)) {
                $guestUp['createtime'] = time();
                $guestUp['updatetime'] = time();
                $guestUp['joinip'] = request()->ip();
                $guestUp['jointime'] = time();
                $guestUp['loginip'] = request()->ip();
                $guestUp['logintime'] = time();
                if ($country) {
                    $guestUp['joincountry'] = $country;
                    $guestUp['logincountry'] = $country;
                }
                $gid = Db::name('guest')->insertGetId($guestUp);
            } else {
                $guestUp['updatetime'] = time();
                if ($guestInfo['loginip'] != request()->ip()) {
                    $guestUp['loginip'] = request()->ip();
                    $guestUp['logintime'] = time();
                    if ($country) {
                        $guestUp['logincountry'] = $country;
                    }
                }
                Db::name('guest')->where('id', $guestInfo['id'])->update($guestUp);
                $gid = $guestInfo['id'];
            }
        }

        //观看权限开始
        $isvip = 1;//1不是vip 2是vip
        $pays = 1; //1不能观看 2能观看
        $payLx = 0; // 购买类型，0未购买，1全章购买，2单集购买
        $uid = 0;
        $midlog = 0;
        if (input('token') && input('token') != 'undefined') {

            $userData = $this->auth->getUserinfo();
            if ($userData) {
                $uid = $userData['id'];
                if ($userData['group_id'] > 1 and $userData['dtime'] >= time()) {
                    $isvip = 2;
                }
                $midlogs = DB::name('videolog')->where(['vid' => $vid, 'uid' => $uid])->order('updatetime desc')->find();
                if ($midlogs) {
                    $midlog = $midlogs['mid'];
                }
                //更新播放记录 开始
                // $up=['uid'=>$uid,'vid'=>$vid,'mid'=>input('mid'),'vtime'=>0,'createtime'=>time(),'updatetime'=>time()];
                // $videolog=Db::name('videolog')->where(['uid'=>$uid,'vid'=>$vid,'mid'=>input('mid')])->find();
                // if($videolog){
                //     Db::name('videolog')->where(['id'=>$videolog['id']])->delete();
                // }
                // Db::name('videolog')->insertGetId($up);
                //更新播放记录 结束

                $config = config('site');
                $config['isfree']=1;
                if ($config['isfree']) {
                    // 免费看剧
                    $pays = 2; // 2能观看
                    $payLx = 1; // 1全章购买
                }
            }

        }

        $datavideo_order = Db::name('video_order')->where(['type' => 2, 'lx' => 1, 'sid' => $vid, 'uid' => $uid])->find();// lx 1 全章购买
        if ($datavideo_order) {
            $pays = 2;
            $payLx = 1;
        } else {
            $dataviod = Db::name('video_order')->where(['type' => 2, 'lx' => 2, 'sid' => $vid, 'uid' => $uid])->find(); //lx 2 单集购买
            if ($dataviod) {
                $payLx = 2;
            }
        }
        //观看权限结束

        $videolist = [];
        if ($list) {
            foreach ($list as $k => $v) {
                $list[$k]['pays'] = $pays;

                //观看权限开始
                if ($v['price'] == 0) {
                    $list[$k]['pays'] = 2;
                }

                if ($v['vipprice'] == 0 and $isvip == 2) {
                    $list[$k]['pays'] = 2;
                }
                $dataviod = Db::name('video_order')->where(['type' => 2, 'lx' => 2, 'sid' => $vid, 'uid' => $uid, 'smid' => $v['id']])->find(); //lx 2 单集购买
                if ($dataviod) {
                    $list[$k]['pays'] = 2;
                }

                $videolist = DB::name('videolist')->where(['id' => $v['pid']])->find();
                if ($videolist['price'] == 0) {
                    $list[$k]['pays'] = 2;
                }
                if ($videolist['vipprice'] == 0 and $isvip == 2) {
                    $list[$k]['pays'] = 2;
                }

                //var_dump($list[$k]['pays']);
                //观看权限结束

                if ($videolist['sum'] <= $total) {
                    $videolist['wanji'] = __('Completed');
                } else {
                    $videolist['wanji'] = __('Updated to ') . $v['name'];
                }

                $videolist['zcount'] = $total;
                $videolove = Db::name('videolove')->where(['uid' => $uid, 'vid' => $v['pid'], 'mid' => $v['id']])->find();
                $list[$k]['like'] = 1;
                if ($videolove) {
                    $list[$k]['like'] = 2;
                }

                $videorecord = Db::name('videorecord')->where(['uid' => $uid, 'vid' => $v['pid'], 'mid' => $v['id']])->find();
                $list[$k]['iszj'] = 1;
                if ($videorecord) {
                    $list[$k]['iszj'] = 2;
                }

                $User = \app\common\model\User::where(['id' => $videolist['uid']])->field('id,nickname,avatar')->find();
                $list[$k]['User'] = $User;

                // $list[$k]['videourl']=$this->pissrc($v['videourl']);
                // $list[$k]['img']=$this->pissrc($v['img']);
                // $list[$k]['nickname']=$User['nickname'];
                // $list[$k]['avatar']=$this->pissrc($User['avatar']);
                // $list[$k]['type']="videoSmallCard";
                // $list[$k]['mid']=$v['id'];

                // $list[$k]['text']=$videolist['text'];
                // $list[$k]['give']=$videolist['give'];
                // $list[$k]['vname']=$videolist['name'];
                // $list[$k]['sum']=$videolist['sum'];

                // $list[$k]['fjname']=$v['name'];
                // $list[$k]['name']=$v['name'].' （共'.$videolist['sum'].'集） 选集 >';
                $list[$k]['priced'] = $v['price'];
                $list[$k]['vippriced'] = $v['vipprice'];

                $list[$k]['priceq'] = $videolist['price'];
                $list[$k]['vippriceq'] = $videolist['vipprice'];
                $list[$k]['isvip'] = $isvip;

                $list[$k]['mid'] = $v['id'];
                $list[$k]['href'] = $this->pissrc($v['img'] . '?x-oss-process=image/resize,l_320');
                $list[$k]['img'] = $this->pissrc($v['img'] . '?x-oss-process=image/resize,l_320');
                $list[$k]['isShowProgressBarTime'] = false;
                $list[$k]['isShowimage'] = false;
                $list[$k]['isplay'] = true;
                // $list[$k]['like']=$videolist['give'];
                $list[$k]['like_n'] = $videolist['give'];
                $list[$k]['msg'] = $videolist['text'];
                $list[$k]['pinlun'] = [];
                $list[$k]['playIng'] = false;
                $list[$k]['playNumber'] = 0;
                $list[$k]['sms_n'] = 0;
                if ($list[$k]['pays'] == 1) {
                    $list[$k]['src'] = '';
                } else {
                    $list[$k]['src'] = $this->pissrc($v['videourl']);

                    $vod = Db::name('vod')->field(['id', 'video_id', 'url', 'updated_at'])->where(['video_id' => $v['id'], 'status' => Vod::VOD_STATUS_PUBLISHED, 'platform' => 'cloudflare'])->find(); //查询可用vod
                    if ($vod) {
                        $list[$k]['vod'] = $vod;
                        if (!empty($list[$k]['src'])) {
                            $list[$k]['src'] = $vod['url'];
                        }
                    } else {
                        $list[$k]['vod'] = null;
                    }
                }

                $list[$k]['state'] = "pause";
                $list[$k]['title'] = '《' . $videolist['name'] . '》';
                $list[$k]['userID'] = !empty($User['id'])?$User['id']:0;
                $list[$k]['username'] = !empty($User['nickname'])?$User['nickname']:'';
                //$list[$k]['_id']=md5($videolist['id']);
                $list[$k]['_id'] = 'vid' . $v['id'];
                $list[$k]['fjname'] = $v['name'];
                $list[$k]['namets'] = $v['name'] . sprintf(__("（Total %d sections） choose >"), $videolist['sum']);
            }
        }
        $result = array("page" => $page, "midlog" => $midlog, "isempty" => !empty($list) ? 1 : 2, "payLx" => $payLx, "data" => $list, "affectedDocs" => $total, "total" => $total, "result" => $list, "videodata" => $videolist, 'tuday' => date('Y-m-d', time()));
        return json($result);
    }

    public function bfjl()
    {
        $vid = input('vid');
        $mid = input('mid');
        $uid = intval(input('uid')); // 用户id
        $guest = trim(input('gid')); // 访客id
        $type = trim(input('type')); // 记录类型
        if ($mid && ($uid || $guest)) {
            //更新播放记录 开始
            $gid = 0;
            if ($guest != '') {
                $guestUp = [
                    'guest' => $guest,
                    'useragent' => substr(request()->server('HTTP_USER_AGENT'), 0, 255),
                    'referer' => $this->request->server('HTTP_REFERER'),
                ];
                $country = null;
//                $ipInfo = geoip(request()->ip())->toArray();
//                if (!empty($ipInfo['iso_code'])) {
//                    $country = $ipInfo['iso_code'];
//                }
                $guestInfo = Db::name('guest')->where(['guest' => $guest])->find();
                if (empty($guestInfo)) {
                    $guestUp['createtime'] = time();
                    $guestUp['updatetime'] = time();
                    $guestUp['joinip'] = request()->ip();
                    $guestUp['jointime'] = time();
                    $guestUp['loginip'] = request()->ip();
                    $guestUp['logintime'] = time();
                    if ($country) {
                        $guestUp['joincountry'] = $country;
                        $guestUp['logincountry'] = $country;
                    }
                    $gid = Db::name('guest')->insertGetId($guestUp);
                } else {
                    $guestUp['updatetime'] = time();
                    if ($guestInfo['loginip'] != request()->ip()) {
                        $guestUp['loginip'] = request()->ip();
                        $guestUp['logintime'] = time();
                        if ($country) {
                            $guestUp['logincountry'] = $country;
                        }
                    }
                    Db::name('guest')->where('id', $guestInfo['id'])->update($guestUp);
                    $gid = $guestInfo['id'];
                }
            }

            $up = ['uid' => $uid, 'gid' => $gid, 'vid' => $vid, 'mid' => $mid, 'vtime' => 0, 'createtime' => time(), 'updatetime' => time(), 'is_end' => $type == 'end' ? 1 : 0];
            $videolog = Db::name('videolog')->where(['uid' => $uid, 'gid' => $gid, 'vid' => $vid, 'mid' => $mid])->find();
            if ($videolog) {
//                Db::name('videolog')->where(['id' => $videolog['id']])->delete();
                unset($up['createtime']);

                if ($videolog['is_end']) {
                    // 不变更完播字段
                    unset($up['is_end']);
                } else {
                    // 记录观看时长
                    $up['vtime'] = time() - $videolog['updatetime'] + $up['vtime'];
                }
                if (empty($videolog['is_end'] && time() - $videolog['updatetime']) >= 20) {
                    // 未完播 && 观影时长大于 20 秒
                    Db::name('videolog')->where(['id' => $videolog['id']])->update($up);

                    $incField = 'plays';
                    if ($type == 'end') {
                        $incField = 'playeds';
                        // 记录完播次数
                        Db::name('videolist')->where(['id' => $vid])->setInc($incField);
                        Db::name('video')->where(['id' => $mid])->setInc($incField);
                    }
                }
            } else {
                Db::name('videolog')->insertGetId($up);

                $incField = 'plays';
//                if ($type == 'end') {
//                    $incField = 'playeds';
//                }
                // 记录播放次数
                Db::name('videolist')->where(['id' => $vid])->setInc($incField);
                Db::name('video')->where(['id' => $mid])->setInc($incField);
            }
            //更新播放记录 结束

            $this->success('success');
        }
    }

    public function zuiju()
    {
        $getweek = $this->get_week();
        $sy = 0;
        $i = 1;
        $tdsy = 0;
        $fdong = 50;
        $chus = 100;
        foreach ($getweek as $k => $v) {
            $qdjl = Db::name('qdjl')->where(['uid' => input('uid'), 'time' => $v['dates']])->find();
            if ($qdjl) {
                $sy = $qdjl['sy'];
                $getweek[$k]['xz'] = 2;
            } else {
                $getweek[$k]['xz'] = 1;
            }

            if ($v['dates'] == date('Y-m-d', time())) {
                if ($sy == 0) {
                    $sy = $chus;
                }
                $tdsy = $sy;
                $getweek[$k]['day'] = '今天';
            }
            $getweek[$k]['discount'] = $sy > 0 ? $sy : $chus;
            if ($sy > 0) {
                $sy = $sy + $fdong;
            }
        }

        $qdjls = Db::name('qdjl')->where(['uid' => input('uid')])->order('id desc')->find();
        if ($qdjls) {
            $qdjl = $qdjls['sum'];
        } else {
            $qdjl = 0;
        }


        $map1['uid'] = input('uid');
        $list = DB::name('videolog')->where($map1)->group('vid')->order('id desc')->limit(12)->select();
        foreach ($list as $k => $v) {
            $v = DB::name('videolog')->where(['vid' => $v['vid'], 'uid' => input('uid')])->order('id desc')->find();
            $list[$k] = $v;

            $videolist = DB::name('videolist')->where(['id' => $v['vid']])->find();
            $list[$k]['vname'] = $videolist['name'];
            $list[$k]['img'] = $this->pissrc($videolist['img']);
            $videocount = DB::name('video')->where(['pid' => $v['vid']])->count();
            $videoweigh = DB::name('video')->where(['pid' => $v['vid']])->order('weigh desc,id desc')->find();
            if ($videolist['sum'] <= $videocount) {
                $list[$k]['mname'] = __('Completed');
            } else {
                $list[$k]['mname'] = __('Updated to ') . $videoweigh['name'];
            }
            $video = DB::name('video')->where(['id' => $v['mid']])->find();
            $list[$k]['dqname'] = __('Watched ') . $video['name'];
        }

        $map2['uid'] = input('uid');
        $record = DB::name('videorecord')->where($map2)->group('vid')->limit(12)->select();
        foreach ($record as $k => $v) {
            $v = DB::name('videorecord')->where(['vid' => $v['vid'], 'uid' => input('uid')])->order('id desc')->find();
            $record[$k] = $v;
            $videolist = DB::name('videolist')->where(['id' => $v['vid']])->find();
            $record[$k]['vname'] = $videolist['name'];
            $record[$k]['img'] = $this->pissrc($videolist['img']);
            $videocount = DB::name('video')->where(['pid' => $v['vid']])->count();
            $videoweigh = DB::name('video')->where(['pid' => $v['vid']])->order('weigh desc,id desc')->find();
            if ($videolist['sum'] <= $videocount) {
                $record[$k]['mname'] = __('Completed');
            } else {
                $record[$k]['mname'] = __('Updated to ') . $videoweigh['name'];
            }
            $video = DB::name('video')->where(['id' => $v['mid']])->find();
            $record[$k]['dqname'] = __('Watched ') . $video['name'];
        }

        $result = array("tdsy" => $tdsy, "qdjl" => $qdjl, "getweek" => $getweek, "new" => $list, "record" => $record, 'config' => $this->config());
        return json($result);
    }

    /**
     * 获取本周所有日期
     */
    public function get_week($time = '', $format = 'Y-m-d')
    {
        $time = $time != '' ? $time : time();
        //获取当前周几
        $week = date('w', $time);
        $date = [];
        for ($i = 1; $i <= 7; $i++) {
            $date[$i - 1]['day'] = date('m.d', strtotime('+' . ($i - $week) . ' days', $time));
            $date[$i - 1]['dates'] = date($format, strtotime('+' . ($i - $week) . ' days', $time));
            $date[$i - 1]['week'] = $i;
        }
        return $date;
    }

    public function wdxh()
    {
        $config = config('site');


        $this->relationSearch = true;
        //如果发送的来源是Selectpage，则转发到Selectpage
        if ($this->request->request('keyField')) {
            return $this->selectpage();
        }
        list($where, $sort, $order, $offset, $limit) = $this->buildparams();
        $map['uid'] = input('uid');
        $order = 'id desc';
        $total = model('Videolove')
            ->with('users')
            ->group('vid')
            ->where($where)
            ->where($map)
            ->count();
        $record = model('Videolove')
            ->with('users')
            ->group('vid')
            ->where($where)
            ->where($map)
            ->order($order)
            ->limit($offset, $limit)
            ->select();
        foreach ($record as $k => $v) {
            $videolist = DB::name('videolist')->where(['id' => $v['vid']])->find();
            $record[$k]['vname'] = $videolist['name'];
            $record[$k]['img'] = $this->pissrc($videolist['img']);
        }

        $result = array("total" => $total, "rows" => $record, 'config' => $this->config());
        return json($result);
    }

    public function recordlist()
    {
        $config = config('site');
        $this->relationSearch = true;
        //如果发送的来源是Selectpage，则转发到Selectpage
        if ($this->request->request('keyField')) {
            return $this->selectpage();
        }
        list($where, $sort, $order, $offset, $limit) = $this->buildparams();
        $map['uid'] = input('uid');
        $order = 'id desc';
        $total = model('Videorecord')
            ->with('users')
            ->group('vid')
            ->where($where)
            ->where($map)
            ->count();
        $record = model('Videorecord')
            ->with('users')
            ->group('vid')
            ->where($where)
            ->where($map)
            ->order($order)
            ->limit($offset, $limit)
            ->select();
        foreach ($record as $k => $v) {
            $v = DB::name('videorecord')->where(['vid' => $v['vid'], 'uid' => input('uid')])->order('id desc')->find();
            $record[$k] = $v;
            $videolist = DB::name('videolist')->where(['id' => $v['vid']])->find();

            $record[$k]['vname'] = $videolist['name'];
            $record[$k]['img'] = $this->pissrc($videolist['img']);
            $videocount = DB::name('video')->where(['pid' => $v['vid']])->count();
            $videoweigh = DB::name('video')->where(['pid' => $v['vid']])->order('weigh desc,id desc')->find();
            if ($videolist['sum'] <= $videocount) {
                $record[$k]['mname'] = __('Completed');
            } else {
                $record[$k]['mname'] = __('Updated to ') . $videoweigh['name'];
            }
            $video = DB::name('video')->where(['id' => $v['mid']])->find();
            $record[$k]['dqname'] = __('Watched ') . $video['name'];

        }

        $result = array("total" => $total, "rows" => $record, 'config' => $this->config());
        return json($result);
    }

    public function videolog()
    {
        $config = config('site');
        $this->relationSearch = true;
        //如果发送的来源是Selectpage，则转发到Selectpage
        if ($this->request->request('keyField')) {
            return $this->selectpage();
        }
        list($where, $sort, $order, $offset, $limit) = $this->buildparams();
        $map['uid'] = input('uid');
        $order = 'id desc';
        $total = model('Videolog')
            ->with('users')
            ->group('vid')
            ->where($where)
            ->where($map)
            ->count();
        $record = model('Videolog')
            ->with('users')
            ->group('vid')
            ->where($where)
            ->where($map)
            ->order($order)
            ->limit($offset, $limit)
            ->select();
        foreach ($record as $k => $v) {
            $v = DB::name('videolog')->where(['vid' => $v['vid'], 'uid' => input('uid')])->order('id desc')->find();
            $record[$k] = $v;
            $videolist = DB::name('videolist')->where(['id' => $v['vid']])->find();

            $record[$k]['vname'] = $videolist['name'];
            $record[$k]['img'] = $this->pissrc($videolist['img']);
            $videocount = DB::name('video')->where(['pid' => $v['vid']])->count();
            $videoweigh = DB::name('video')->where(['pid' => $v['vid']])->order('weigh desc,id desc')->find();
            if ($videolist['sum'] <= $videocount) {
                $record[$k]['mname'] = __('Completed');
            } else {
                $record[$k]['mname'] = __('Updated to ') . $videoweigh['name'];
            }
            $video = DB::name('video')->where(['id' => $v['mid']])->find();
            $record[$k]['dqname'] = __('Watched ') . $video['name'];

        }

        $result = array("total" => $total, "rows" => $record, 'config' => $this->config());
        return json($result);
    }

    public function indexdata()
    {
        $map1 = ['videolist.lx' => 1, 'videolist.status' => 'normal'];
        $list = model('Videolist')->with('users')->where($map1)->order('videolist.createtime desc')->limit(12)->select();

        $mapjq = ['videolist.lx' => 1, 'videolist.status' => 'normal'];
        $mapjq['videolist.story'] = ['neq', ''];
        $listjq = model('Videolist')->with('users')->where($mapjq)->order('videolist.createtime desc')->limit(50)->select();

        $map2 = ['videolist.lx' => 1, 'videolist.status' => 'normal'];
        $hotdata = model('Videolist')->with('users')->where($map2)->order('videolist.views desc')->limit(12)->select();

        $map3 = ['videolist.lx' => 1, 'videolist.status' => 'normal'];
        $givedata = model('Videolist')->with('users')->where($map3)->order('videolist.give desc')->limit(12)->select();

        $mapbz = ['videolist.lx' => 4, 'videolist.status' => 'normal'];
        $listbz = model('Videolist')->with('users')->where($mapbz)->order('videolist.id desc')->limit(50)->select();

        $result = array("listbz" => $this->listadt($listbz), "new" => $this->listadt($list), "listjq" => $this->listadt($listjq), "hotdata" => $this->listadt($hotdata), "givedata" => $this->listadt($givedata), 'config' => $this->config());

        $fenleiData = null;
        $configFenlei = $result['config']['fenlei'];
        foreach ($configFenlei as $fenlei) {
            $map = ['videolist.lx' => 1, 'videolist.status' => 'normal'];
            $map['videolist.type'] = ['like', '%"' . $fenlei['id'] . '"%'];
            $fenleilist = model('Videolist')->with('users')->where($map)->order('videolist.sort desc')->limit(6)->select();

            $fenleiItem = null;
            $fenleiItem['fenlei'] = $fenlei;
            $fenleiItem['list'] = $this->listadt($fenleilist);

            $fenleiData[] = $fenleiItem;
        }

        $result['fenleiData'] = $fenleiData;

        return json($result);
    }

    public function listadt($list)
    {
        if ($list) {
            $config = config('site');
            foreach ($list as $k => $v) {
                $list[$k]['updatetime'] = date('Y-m-d', $v['updatetime']);
                $lx = [1 => __('Video'), 2 => __('Audio'), 3 => __('Novel'), 4 => __('Image')];
                $list[$k]['lxname'] = isset($lx[$list[$k]['lx']]) ? $lx[$list[$k]['lx']] : __('Other');

                if (isset($v['users']['avatar'])) {
                    if (strpos($v['users']['avatar'], 'http') !== false) {
                        $list[$k]['avatar'] = $v['users']['avatar'];
                    } else {
                        if ($v['users']['avatar']) {
                            $list[$k]['avatar'] = $config['imgurl'] . $v['users']['avatar'];
                        } else {
                            $list[$k]['avatar'] = $config['imgurl'] . '/uploads/face/default.png';
                        }
                    }
                } else {
                    $list[$k]['avatar'] = $config['imgurl'] . '/uploads/face/default.png';
                }
                if (strpos($v['img'], 'http') !== false) {
                    $list[$k]['img'] = $v['img'];
                } else {
                    if ($v['img']) {
                        $list[$k]['img'] = $config['imgurl'] . $v['img'] . '?x-oss-process=image/resize,l_320';
                    } else {
                        $list[$k]['img'] = $config['imgurl'] . '/uploads/default.jpg' . '?x-oss-process=image/resize,l_320';
                    }
                }
            }
        }
        return $list;
    }

    public function lists()
    {
        $config = config('site');
        $this->relationSearch = true;
        //如果发送的来源是Selectpage，则转发到Selectpage
        if ($this->request->request('keyField')) {
            return $this->selectpage();
        }
        list($where, $sort, $order, $offset, $limit) = $this->buildparams();
        if (input('uid', 0) > 0) {
            $map['uid'] = input('uid');
        }
        $order = 'videolist.sort desc,videolist.give desc';
        if (input('ishot')) {
            $map['videolist.ishot'] = input('ishot');
        }
        // if(input('lx')){
        //     $map['videolist.lx']=input('lx');
        // }
        if (input('lx') == 3) {
            $order = 'videolist.give desc';
        } else if (input('lx') == 2) {
            $map['videolist.price'] = 0;
        }
        $map['videolist.lx'] = 1;
        if (input('typeid')) {
            $map['videolist.type'] = ['like', '%"' . input('typeid') . '"%'];
        }
        if (input('addddid')) {
            $map['videolist.adddd'] = ['like', '%"' . input('addddid') . '"%'];
        }
        if (input('yearid')) {
            $map['videolist.yearq'] = input('yearid');
        }
        if (input('type')) {
            if (input('type') == __('All')) {

            } else {
                $map['videolist.type'] = input('type');
            }
        }
        if (input('keytext')) {
            $map['videolist.name|videolist.id'] = ['like', '%' . input('keytext') . '%'];
        }
        $map['videolist.status'] = 'normal';
        $total = model('Videolist')
            ->with('users')
            ->where($where)
            ->where($map)
            ->count();
        $list = model('Videolist')
            ->with('users')
            ->where($where)
            ->where($map)
            ->order($order)
            ->limit($offset, $limit)
            ->select();
        if ($list) {
            foreach ($list as $k => $v) {
                $list[$k]['updatetime'] = date('Y-m-d', $v['updatetime']);
                $lx = [1 => __('Video'), 2 => __('Audio'), 3 => __('Novel'), 4 => __('Image')];
                $list[$k]['lxname'] = isset($lx[$list[$k]['lx']]) ? $lx[$list[$k]['lx']] : __('Other');

                if (isset($v['users']['avatar'])) {
                    if (strpos($v['users']['avatar'], 'http') !== false) {
                        $list[$k]['avatar'] = $v['users']['avatar'];
                    } else {
                        if ($v['users']['avatar']) {
                            $list[$k]['avatar'] = $config['imgurl'] . $v['users']['avatar'];
                        } else {
                            $list[$k]['avatar'] = $config['imgurl'] . '/uploads/face/default.png';
                        }
                    }
                } else {
                    $list[$k]['avatar'] = $config['imgurl'] . '/uploads/face/default.png';
                }
                if (strpos($v['img'], 'http') !== false) {
                    $list[$k]['img'] = $v['img'];
                } else {
                    if ($v['img']) {
                        $list[$k]['img'] = $config['imgurl'] . $v['img'] . '?x-oss-process=image/resize,l_320';
                    } else {
                        $list[$k]['img'] = $config['imgurl'] . '/uploads/default.jpg' . '?x-oss-process=image/resize,l_320';
                    }
                }
            }
        }
        $typedata = ['type' => $this->fenlei('type'), 'adddd' => $this->fenlei('adddd'), 'year' => $this->fenlei('year'), 'type2' => $this->fenlei('type2'), 'type3' => $this->fenlei('type3'), 'type4' => $this->fenlei('type4')];
        $result = array("total" => $total, "rows" => $list, 'config' => $this->config(), 'typedata' => $typedata);
        return json($result);
    }

    public function info()
    {
        $mid = input('mid', 0);
        $config = config('site');
        //var_dump($where);
        $isvip = 0;
        $Userdata = [];
        $datavideo_order = [];
        $datavideo_orderd = [];
        if (input('token')) {
            $Userdata = $this->auth->getUserinfo();
            if ($Userdata['group_id'] == 1) {
                $isvip = 0;
            } else {
                if ($Userdata['dtime'] >= time()) {
                    $isvip = 2;
                } else {
                    $isvip = 1;
                }
            }
        }

        $list = model('Videolist')
            ->where(['id' => input('id')])
            ->find();
        if ($list) {
            $list['updatetime'] = date('Y-m-d', $list['updatetime']);
            if (strpos($list['img'], 'http') !== false) {
                $list['img'] = $list['img'];
            } else {
                if ($list['img']) {
                    $list['img'] = $config['imgurl'] . $list['img'] . '?v=' . time();
                } else {
                    $list['img'] = $config['imgurl'] . '/uploads/20200523/250b3f89b40ff3714b07cc51b4c2f63d.png';
                }
            }
        }

        $price = $list['price'] * 1;
        $vipprice = $list['vipprice'] * 1;

        if ($Userdata) {
            $datavideo_order = Db::name('video_order')->where(['type' => 2, 'lx' => 1, 'sid' => input('id'), 'uid' => $Userdata['id']])->find();//1 全章购买
        }

        $orders = 'weigh asc';
        $Video = model('Video')
            ->where(['pid' => input('id')])
            ->order($orders)
            ->select();
        if ($Video) {
            foreach ($Video as $k => $v) {
                $Video[$k]['updatetime'] = date('Y-m-d', $v['updatetime']);

                if (strpos($v['img'], 'http') !== false) {
                    $Video[$k]['img'] = $v['img'];
                } else {
                    if ($v['img']) {
                        $Video[$k]['img'] = $config['imgurl'] . $v['img'];
                    } else {
                        $Video[$k]['img'] = $config['imgurl'] . '/uploads/20200523/250b3f89b40ff3714b07cc51b4c2f63d.png';
                    }
                }
                if (strpos($v['videourl'], 'http') !== false) {
                    $Video[$k]['videourl'] = $v['videourl'];
                } else {
                    if ($v['videourl']) {
                        $Video[$k]['videourl'] = $config['imgurl'] . $v['videourl'];
                    } else {
                        $Video[$k]['videourl'] = '';
                    }
                }
                if ($mid == $Video[$k]['id']) {
                    $list['videourl'] = $Video[$k]['videourl'];
                    $list['pid'] = $Video[$k]['id'];
                    $list['ji'] = $Video[$k]['name'];
                    $list['playIndex'] = $k;
                    $list['infos'] = $Video[$k]['info'];
                    $list['priced'] = $Video[$k]['price'];
                    $list['vippriced'] = $Video[$k]['vipprice'];
                    $list['syj'] = isset($Video[$k - 1]['id']) ? $Video[$k - 1]['id'] : 0;
                    $list['xyj'] = isset($Video[$k + 1]['id']) ? $Video[$k + 1]['id'] : 0;
                } else if ($mid == 0) {
                    if ($v['weigh'] == 1) {
                        $list['videourl'] = $Video[$k]['videourl'];
                        $list['pid'] = $Video[$k]['id'];
                        $list['ji'] = $Video[$k]['name'];
                        $list['playIndex'] = 0;
                        $list['infos'] = $Video[$k]['info'];
                        $list['priced'] = $Video[$k]['price'];
                        $list['vippriced'] = $Video[$k]['vipprice'];
                        $list['syj'] = 0;
                        $list['xyj'] = isset($Video[$k + 1]['id']) ? $Video[$k + 1]['id'] : 0;
                    }
                }


                $Video[$k]['isplay'] = 0;
                $dataviod = [];
                if ($Userdata) {
                    $dataviod = Db::name('video_order')->where(['type' => 2, 'lx' => 2, 'sid' => input('id'), 'uid' => $Userdata['id'], 'smid' => $Video[$k]['id']])->find(); //单集购买
                }

                if ($price == 0) {//判断是否全集购买
                    $Video[$k]['isplay'] = 1;
                } else if ($isvip > 1 and $vipprice == 0) {
                    $Video[$k]['isplay'] = 1;
                } else if ($datavideo_order) {
                    $Video[$k]['isplay'] = 1;
                }

                if ($Video[$k]['price'] == 0) {//判断是否单集购买
                    $Video[$k]['isplay'] = 1;
                } else if ($isvip > 1 and $Video[$k]['vipprice'] == 0) {
                    $Video[$k]['isplay'] = 1;
                } else if ($dataviod) {
                    $Video[$k]['isplay'] = 1;
                }
                if ($Video[$k]['isplay'] == 0) {
                    $Video[$k]['src'] = $config['imgurl'] . '/assets/mp3/1b626083-179ccf4b36011.wav?v=' . $Video[$k]['id'];
                    $Video[$k]['title'] = $Video[$k]['name'] . ' 【未购买】';
                } else {
                    $Video[$k]['src'] = $Video[$k]['videourl'];
                    $Video[$k]['title'] = $Video[$k]['name'];//$list['name'].' '.
                }
                $Video[$k]['singer'] = $list['uname'];
                $Video[$k]['coverImgUrl'] = $Video[$k]['img'];

            }
        }
        if ($mid == 0) {
            $mid = $list['pid'];
        }
        if ($Userdata) {
            $datavideo_orderd = Db::name('video_order')->where(['type' => 2, 'lx' => 2, 'sid' => input('id'), 'uid' => $Userdata['id'], 'smid' => $mid])->find(); //2单集购买
        }
        $list['video'] = $Video;
        $lx = [1 => __('Video'), 2 => __('Audio'), 3 => __('Article'), 3 => __('Image')];
        $list['lxname'] = isset($lx[$list['lx']]) ? $lx[$list['lx']] : __('Other');

        $priced = $list['priced'] * 1;
        $vippriced = $list['vippriced'] * 1;

        $isplay = 0;
        $isplayd = 0;
        if ($price == 0) {
            $isplay = 1;
        } else if ($isvip > 1 and $vipprice == 0) {
            $isplay = 1;
        } else if ($datavideo_order) {
            $isplay = 1;
        }

        if ($priced == 0) {
            //$isplay=1;
            $isplayd = 1;
        } else if ($isvip > 1 and $vippriced == 0) {
            //$isplay=1;
            $isplayd = 1;
        } else if ($datavideo_orderd) {
            //$isplay=1;
            $isplayd = 1;
        }
        $list['isplayd'] = $isplayd;
        $list['isplay'] = $isplay;
        $list['isvip'] = $isvip;
        $list['mrseek'] = isset($config['mrseek']) ? $config['mrseek'] : 15;
        return json($list);
    }

    public function infotp()
    {
        $mid = input('mid', 0);
        $config = config('site');
        //var_dump($where);
        $isvip = 0;
        $Userdata = [];
        $datavideo_order = [];
        $datavideo_orderd = [];
        if (input('token')) {
            $Userdata = $this->auth->getUserinfo();
            if ($Userdata['group_id'] == 1) {
                $isvip = 0;
            } else {
                if ($Userdata['dtime'] >= time()) {
                    $isvip = 2;
                } else {
                    $isvip = 1;
                }
            }
        }

        $list = model('Videolist')
            ->where(['id' => input('id')])
            ->find();
        if ($list) {
            $list['updatetime'] = date('Y-m-d', $list['updatetime']);
            if (strpos($list['img'], 'http') !== false) {
                $list['img'] = $list['img'];
            } else {
                if ($list['img']) {
                    $list['img'] = $config['imgurl'] . $list['img'] . '?v=' . time();
                } else {
                    $list['img'] = $config['imgurl'] . '/uploads/20200523/250b3f89b40ff3714b07cc51b4c2f63d.png';
                }
            }
        }

        $price = $list['price'] * 1;
        $vipprice = $list['vipprice'] * 1;

        if ($Userdata) {
            $datavideo_order = Db::name('video_order')->where(['type' => 2, 'lx' => 1, 'sid' => input('id'), 'uid' => $Userdata['id']])->find();//1 全章购买
        }


        if ($Userdata) {
            $datavideo_orderd = Db::name('video_order')->where(['type' => 2, 'lx' => 2, 'sid' => input('id'), 'uid' => $Userdata['id'], 'smid' => $mid])->find(); //2单集购买
        }
        $ud = [];
        if ($list['pic']) {
            $arr = explode(',', $list['pic']);
            foreach ($arr as $ka => $va) {
                $ud[] = $this->pissrc($va);
            }
        }
        $list['images'] = $ud;
        $list['imageslength'] = count($ud);
        //$this->pissrc($v['videourl']);


        $lx = [1 => __('Video'), 2 => __('Audio'), 3 => __('Article'), 3 => __('Image')];
        $list['lxname'] = isset($lx[$list['lx']]) ? $lx[$list['lx']] : __('Other');


        $isplay = 0;
        $isplayd = 0;
        if ($price == 0) {
            $isplay = 1;
        } else if ($isvip > 1 and $vipprice == 0) {
            $isplay = 1;
        } else if ($datavideo_order) {
            $isplay = 1;
        } else if ($isvip > 1) {
            //$isplay=1;
            $isplayd = 1;
        } else if ($datavideo_orderd) {
            //$isplay=1;
            $isplayd = 1;
        }
        $list['videoids'] = isset($config['weixinxcx']['videoAd']) ? $config['weixinxcx']['videoAd'] : '';
        $list['isplayd'] = $isplayd;
        $list['isplay'] = $isplay;
        $list['isvip'] = $isvip;
        $list['mrseek'] = isset($config['mrseek']) ? $config['mrseek'] : 15;
        return json($list);
    }

    public function orderlists()
    {
        $config = config('site');
        $this->relationSearch = true;
        //如果发送的来源是Selectpage，则转发到Selectpage
        if ($this->request->request('keyField')) {
            return $this->selectpage();
        }
        list($where, $sort, $order, $offset, $limit) = $this->buildparams();
        $order = 'video_order.id desc';
        $map['users.id'] = input('uid');
        $map['video_order.type'] = 2;
        $total = model('VideoOrder')
            ->with('users')
            ->where($where)
            ->where($map)
            ->count();
        $list = model('VideoOrder')
            ->with('users')
            ->where($where)
            ->where($map)
            ->order($order)
            ->limit($offset, $limit)
            ->select();
        if ($list) {
            foreach ($list as $k => $v) {
                //var_dump($v['img']);
                $videolists = Db::name('videolist')->where(['id' => $v['sid']])->find();
                $splx = isset($videolists['lx']) ? $videolists['lx'] : 0;
                $list[$k]['time'] = date('Y-m-d', $v['updatetime']);
                if ($v['type'] == 1) {
                    $list[$k]['status_means'] = __('Unpaid');//1未支付 2已支付
                } else if ($v['type'] == 2) {
                    $list[$k]['status_means'] = __('Paid');
                }
                $list[$k]['status'] = 1;
                $list[$k]['exchange_type'] = 3;
                $list[$k]['delivery_type'] = 1;
                $list[$k]['tp'] = 1;
                $list[$k]['splx'] = $splx;

                //$list[$k]['name']=substr($v['name'],0,10);
            }
        }

        $result = array("total" => $total, "rows" => $list, 'config' => $this->config());
        return json($result);
    }

    public function tjinfo()
    {
        $config = config('site');
        //var_dump($where);

        $list = model('Task')
            ->where(['id' => input('id')])
            ->find();
        if ($list) {
            $odcount = model('TaskOrder')->where(['oid' => $list['id'], 'uid' => input('uid', 0)])->count();
            $TaskOrder = model('TaskOrder')->where(['id' => input('rwid', 0)])->find();
            if ($TaskOrder) {
                if ($TaskOrder['buz']) {
                    $TaskOrder['buz'] = json_decode($TaskOrder['buz'], true);
                }
            }
            $list['TaskOrder'] = $TaskOrder;
            if ($list['idsum'] > $odcount) {
                $list['islname'] = 1;
            } else {
                $list['islname'] = 2;
            }
            if ($list['status'] == 'hidden') {
                $list['islname'] = 3;
            }
            $list['odcount'] = $odcount;
            $UserGroup = model('UserGroup')->where(['id' => $list['users']['group_id']])->find();
            if ($list['buz']) {
                $list['buz'] = json_decode($list['buz'], true);
            } else {
                $list['buz'] = [];
            }
            if ($TaskOrder['buz']) {
                $list['buz'] = $TaskOrder['buz'];
            }
            if (isset($UserGroup['name'])) {
                $list['Groupname'] = $UserGroup['name'];
            } else {
                $list['Groupname'] = '';
            }
            $list['createtime'] = date('Y-m-d H:i', $list['createtime']);
            if (isset($list['users']['avatar'])) {
                if (strpos($list['users']['avatar'], 'http') !== false) {
                    $list['avatar'] = $list['users']['avatar'];
                } else {
                    if ($list['users']['avatar']) {
                        $list['avatar'] = $config['imgurl'] . $list['users']['avatar'];
                    } else {
                        $list['avatar'] = $config['imgurl'] . '/uploads/20200523/250b3f89b40ff3714b07cc51b4c2f63d.png';
                    }
                }
            } else {
                $list['avatar'] = $config['imgurl'] . '/uploads/20200523/250b3f89b40ff3714b07cc51b4c2f63d.png';
            }

        }
        return json($list);
    }

    public function fenlei($tpie = 'type')
    {
        $tree = Tree::instance();
        $this->model = model('app\common\model\Category');
        $tree->init(collection($this->model->where(['type' => $tpie])->order('weigh desc,id desc')->field('id,pid,name,nickname,image,type,flag')->select())->toArray(), 'pid');
        $fenlei = $tree->getTreeList($tree->getTreeArray(0), 'name');
        $groupdata[0] = ['name' => __('All'), 'id' => 0];
        foreach ($fenlei as $k => $v) {
            $groupdata[$k + 1]['name'] = $v['name'];
            $groupdata[$k + 1]['id'] = $v['id'];
        }
        return $groupdata;
    }

    public function vod_notify()
    {
        $result = file_get_contents("php://input");
        Log::info('vod_notify:' . $result);
        if (is_string($result)) {
            $result = json_decode($result, true);
        }
        Log::info('vod_notify Message:' . $result['Message']);
        $message = json_decode($result['Message'], true);

        if ($message) {
            if (isset($message['status']) && $message['status'] == 'Ingest') {
                // 开始转码,记录guid
                $video = Db::name('video')->where('videourl', 'like', '%' . $message['srcVideo'])->find();
                $videoList = Db::name('videolist')->where('id', $video['pid'])->find();
                $video['videourl'] = fullMediaUrl($video['videourl']);
                $title = $videoList['name'] . '-' . $video['name'];
                if ($video) {
                    Vod::addVod($video['id'], $video['pid'], $title, 'M3U8', null, null, $message['guid']);
                }
            } else if (isset($message['workflowStatus']) && $message['workflowStatus'] == 'Complete') {
                // 转码结束
                $vod = Db::name('vod')->where('guid', $message['guid'])->find();
                if ($vod) {
                    Vod::where('id', '=', $vod['id'])->update(['url' => $message['hlsUrl'], 'status' => Vod::VOD_STATUS_PUBLISHED]);
                }
            } else {
                echo json_encode($message);
            }
        } else {
            echo 'no message';
        }

    }
}
